/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.environment.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.kopsox.environment.ValidatorEnvironment;
import org.kopsox.environment.proxy.EnvironmentPropertiesEnum;
import org.kopsox.util.xml.data.XMLValidatorDescriptor;
import org.kopsox.util.xml.parser.XMLParserFactory;
import org.kopsox.util.xml.parser.exception.KopsoxXMLParserException;
import org.kopsox.util.xml.parser.impl.ValidatorXMLParser;
import org.kopsox.validation.Validator;
import org.kopsox.validation.handler.ValidationErrorHandler;

/**
 * @author Konrad Renner - 15.10.2010
 *
 */
public class ValidatorEnvironmentImpl implements ValidatorEnvironment {
	
	private final Map<String, XMLValidatorDescriptor> validators;
	
	private volatile boolean initialized;
	
	public ValidatorEnvironmentImpl(){
		this.validators = new HashMap<String, XMLValidatorDescriptor>();
		this.initialized = false;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.environment.KopsoxEnvironment#initialize()
	 */
	@Override
	public synchronized boolean initialize(final Properties properties) {
		if(!this.initialized && properties != null){
			String pathToXml = properties.getProperty(EnvironmentPropertiesEnum.PATHTO_VALIDATOR_XML.toString());
			
			if(pathToXml == null){
				return false;
			}
			
			try {
				ValidatorXMLParser parser = XMLParserFactory.getXMLParser(ValidatorXMLParser.class, pathToXml);
				
				parser.parseXML();
				
				List<XMLValidatorDescriptor> descriptors = parser.getValidatorDescriptors();
				
				for(XMLValidatorDescriptor desc: descriptors){
					validators.put(desc.getName(), desc);
				}
				
				this.initialized = true;
			} catch (KopsoxXMLParserException e) {
				e.printStackTrace();
				return false;
			}
		}
		
		return this.initialized;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.environment.ValidatorEnvironment#getValidator(java.lang.String)
	 */
	@Override
	public Validator getValidator(final String name) {
		
		if(!this.initialized || !this.validators.containsKey(name)){
			return null;
		}
		
		XMLValidatorDescriptor desc = this.validators.get(name);
		
		
		try {
			Class<?> clazz = Class.forName(desc.getClassName());
			
			Validator validator = (Validator) clazz.newInstance();
			
			//Add the handlers
			for(String handler:desc.getHandlers()){
				
				Class<?> handlerClazz = Class.forName(handler);
				
				validator.addHandler((ValidationErrorHandler) handlerClazz.newInstance());
			}
			
			return validator;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return null;
	}

}
